﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using YArchitech.LIB;

namespace YArchitech.DataModel
{
	public class GarageLineSetAuxiliary
	{
		public GarageLineSetAuxiliary()
		{
			if (!File.Exists(this.xmlFilePath))
			{
				File.AppendAllText(this.xmlFilePath, File.ReadAllText(this.backUpXmlFilePath));
			}
			this.xmlDoc = new XmlDocument();
			this.xmlDoc.Load(this.xmlFilePath);
			this.xmlNode = this.xmlDoc.DocumentElement;
		}

		public List<GarageLineSetting> LoadGarageLineData()
		{
			List<GarageLineSetting> list = new List<GarageLineSetting>();
			foreach (object obj in this.xmlNode.ChildNodes)
			{
				XmlNode xmlNode = (XmlNode)obj;
				GarageLineSetting garageLineSetting = new GarageLineSetting();
				garageLineSetting.ID = Convert.ToInt32(xmlNode.Attributes["ID"].Value);
				garageLineSetting.Min = xmlNode.Attributes["Min"].Value;
				try
				{
					double num = Convert.ToDouble(xmlNode.Attributes["Max"].Value);
					if (num > 0.0)
					{
						garageLineSetting.Max = num.ToString();
					}
					else
					{
						garageLineSetting.Max = " ";
					}
				}
				catch (Exception ex)
				{
					garageLineSetting.Max = " ";
					string message = ex.Message;
				}
				int lightCount = Convert.ToInt32(xmlNode.Attributes["LightCount"].Value);
				garageLineSetting.LightCount = lightCount;
				list.Add(garageLineSetting);
			}
			return list;
		}

		public List<GarageLineSetting> GetGarageLineInfosByLayStyle(bool beLayStyle)
		{
			List<GarageLineSetting> list = new List<GarageLineSetting>();
			foreach (object obj in this.xmlNode.ChildNodes)
			{
				XmlNode xmlNode = (XmlNode)obj;
				GarageLineSetting garageLineSetting = new GarageLineSetting();
				garageLineSetting.ID = Convert.ToInt32(xmlNode.Attributes["ID"].Value);
				if ((!beLayStyle || garageLineSetting.ID <= 20) && (beLayStyle || garageLineSetting.ID >= 20))
				{
					garageLineSetting.Min = xmlNode.Attributes["Min"].Value;
					try
					{
						double num = Convert.ToDouble(xmlNode.Attributes["Max"].Value);
						if (num > 0.0)
						{
							garageLineSetting.Max = num.ToString();
						}
					}
					catch (Exception ex)
					{
						garageLineSetting.Max = " ";
						string message = ex.Message;
					}
					int lightCount = Convert.ToInt32(xmlNode.Attributes["LightCount"].Value);
					garageLineSetting.LightCount = lightCount;
					list.Add(garageLineSetting);
				}
			}
			return list;
		}

		public void SaveGarageLine(List<GarageLineSetting> listGarageLine)
		{
			this.xmlDoc.DocumentElement.RemoveAll();
			foreach (GarageLineSetting garageLineSetting in listGarageLine)
			{
				XmlElement xmlElement = this.xmlDoc.CreateElement("Setting");
				xmlElement.SetAttribute("ID", garageLineSetting.ID.ToString());
				xmlElement.SetAttribute("Min", garageLineSetting.Min.ToString());
				if (!string.IsNullOrEmpty(garageLineSetting.Max))
				{
					xmlElement.SetAttribute("Max", garageLineSetting.Max.ToString());
				}
				xmlElement.SetAttribute("LightCount", garageLineSetting.LightCount.ToString());
				this.xmlNode.AppendChild(xmlElement);
			}
			this.xmlDoc.Save(this.xmlFilePath);
		}

		private XmlDocument xmlDoc;

		private XmlNode xmlNode;

		private string backUpXmlFilePath = Path.Combine(Product.DataLocation, "GarageLineSetting.xml");

		private string xmlFilePath = Path.Combine(Product.UserDataLocation, "GarageLineSetting.xml");
	}
}
